import UIKit
import SnapKit

class MarkdownTestCell: UITableViewCell {
    static let reuseIdentifier = "MarkdownTestCell"
    
    // MARK: - UI Components
    private let containerView: UIView = {
        let view = UIView()
        view.backgroundColor = .systemBackground
        view.layer.cornerRadius = 12
        view.layer.shadowColor = UIColor.black.cgColor
        view.layer.shadowOffset = CGSize(width: 0, height: 2)
        view.layer.shadowRadius = 4
        view.layer.shadowOpacity = 0.1
        return view
    }()
    
    private let titleLabel: UILabel = {
        let label = UILabel()
        label.font = .systemFont(ofSize: 17, weight: .semibold)
        label.textColor = .label
        return label
    }()
    
    private let previewLabel: UILabel = {
        let label = UILabel()
        label.font = .systemFont(ofSize: 14)
        label.textColor = .secondaryLabel
        label.numberOfLines = 2
        return label
    }()
    
    private let iconView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.tintColor = .systemBlue
        return imageView
    }()
    
    // MARK: - Initialization
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Setup
    private func setupUI() {
        backgroundColor = .clear
        selectionStyle = .none
        
        contentView.addSubview(containerView)
        containerView.addSubview(iconView)
        containerView.addSubview(titleLabel)
        containerView.addSubview(previewLabel)
        
        containerView.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16))
        }
        
        iconView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
            make.size.equalTo(24)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(12)
            make.leading.equalTo(iconView.snp.trailing).offset(12)
            make.trailing.equalToSuperview().offset(-16)
        }
        
        previewLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
            make.leading.trailing.equalTo(titleLabel)
            make.bottom.equalToSuperview().offset(-12)
        }
    }
    
    // MARK: - Configuration
    func configure(with title: String, content: String, type: MarkdownTestType) {
        titleLabel.text = title
        previewLabel.text = String(content.prefix(100))
        iconView.image = type.icon
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        // 更新阴影颜色以适应深色模式
        containerView.layer.shadowColor = UIColor.black.cgColor
    }
}

// MARK: - Markdown Test Types
enum MarkdownTestType {
    case heading
    case formatting
    case list
    case linkAndImage
    case codeBlock
    case blockquote
    case table
    case horizontalRule
    case mixed
    case specialCharacters
    case customTag
    
    var icon: UIImage? {
        let configuration = UIImage.SymbolConfiguration(pointSize: 16, weight: .medium)
        switch self {
        case .heading:
            return UIImage(systemName: "textformat.size", withConfiguration: configuration)
        case .formatting:
            return UIImage(systemName: "textformat", withConfiguration: configuration)
        case .list:
            return UIImage(systemName: "list.bullet", withConfiguration: configuration)
        case .linkAndImage:
            return UIImage(systemName: "link", withConfiguration: configuration)
        case .codeBlock:
            return UIImage(systemName: "chevron.left.forwardslash.chevron.right", withConfiguration: configuration)
        case .blockquote:
            return UIImage(systemName: "text.quote", withConfiguration: configuration)
        case .table:
            return UIImage(systemName: "tablecells", withConfiguration: configuration)
        case .horizontalRule:
            return UIImage(systemName: "minus", withConfiguration: configuration)
        case .mixed:
            return UIImage(systemName: "doc.richtext", withConfiguration: configuration)
        case .specialCharacters:
            return UIImage(systemName: "character", withConfiguration: configuration)
        case .customTag:
            return UIImage(systemName: "tag", withConfiguration: configuration)
        }
    }
}
